package com.tbvtc.agriculturalcreditchainserver2025.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tbvtc.agriculturalcreditchainserver2025.dto.MapImage.getMapImageDto;
import com.tbvtc.agriculturalcreditchainserver2025.entity.Crop;
import com.tbvtc.agriculturalcreditchainserver2025.entity.FarmlandInfo;
import com.tbvtc.agriculturalcreditchainserver2025.entity.User;
import com.tbvtc.agriculturalcreditchainserver2025.mapper.CropMapper;
import com.tbvtc.agriculturalcreditchainserver2025.mapper.FarmlandInfoMapper;
import com.tbvtc.agriculturalcreditchainserver2025.mapper.UserMapper;
import com.tbvtc.agriculturalcreditchainserver2025.service.ICropService;
import com.tbvtc.agriculturalcreditchainserver2025.service.MapImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MapImageServiceImpl implements MapImageService {

    @Autowired
    private CropMapper cropMapper ;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private FarmlandInfoMapper farmlandInfoMapper;
    /**
     * 查询地图图片
     * @param id
     * @return
     */
    @Override
    public List getMapImage() {
//        Crop crop = cropMapper.selectById(id);
        LambdaQueryWrapper<Crop> cropWrapper = new LambdaQueryWrapper<>();
        List<Crop> crops = cropMapper.selectList(cropWrapper);
        List<getMapImageDto> MapImageDos = BeanUtil.copyToList(crops, getMapImageDto.class);
//        getMapImageDto imageDto = BeanUtil.copyProperties(crop, getMapImageDto.class);
        // 查询土地名称对应的经纬度
//        crops.forEach(crop -> {
//
//            MapImageDos.forEach(MapImageDo -> {
//                LambdaQueryWrapper<FarmlandInfo> framerInfoWrapper = new LambdaQueryWrapper<>();
//                framerInfoWrapper.eq(FarmlandInfo::getLandName, crop.getLandName());
//                FarmlandInfo farmlandInfo = farmlandInfoMapper.selectOne(framerInfoWrapper);
//                //查询名字
//                User user = userMapper.selectById(farmlandInfo.getRoleId());
//                MapImageDo.setLatitude(farmlandInfo.getLatitude());
//                MapImageDo.setLongitude(farmlandInfo.getLongitude());
//                MapImageDo.setAddress(farmlandInfo.getLandAddress());
//                MapImageDo.setUserName(user.getUsername());
//            });
//        });
        MapImageDos.forEach(MapImageDo -> {
            Crop crop = cropMapper.selectById(MapImageDo.getId());
            LambdaQueryWrapper<FarmlandInfo> framerInfoWrapper = new LambdaQueryWrapper<>();
            framerInfoWrapper.eq(FarmlandInfo::getLandName, crop.getLandName());
            FarmlandInfo farmlandInfo = farmlandInfoMapper.selectOne(framerInfoWrapper);
            //查询名字
                User user = userMapper.selectById(farmlandInfo.getRoleId());
                MapImageDo.setLatitude(farmlandInfo.getLatitude());
                MapImageDo.setLongitude(farmlandInfo.getLongitude());
                MapImageDo.setAddress(farmlandInfo.getLandAddress());
                MapImageDo.setUserName(user.getUsername());
        });


        return MapImageDos;
    }

}
